Prototype multiple receivers
日付:2021/10/14
URL:https://youtrack.jetbrains.com/issue/KT-42435
調査者:mkeeda
カテゴリ:Kotlin
一言で表すと
議論の発端は
code:kotlin
interface Extention
interface Context {
fun Extention.f()
}
があったとき、fun Extention.f() を Context の外で書けないのを問題として捉えている
それを解決するのが Multiple recievers
概要
進捗
Context receivers という形でデザインのプロポーザルが出た
プロポーザル文
https://github.com/Kotlin/KEEP/blob/a909d00ca114d2e418d3cf9c4513a534f8ad1751/proposals/context-receivers.md
プロポーザルに対する議論
https://github.com/Kotlin/KEEP/issues/259
ざっくりと文法
context() でReceiver を外から付与できるらしい
code:kotlin
context(Scope)
fun Entity.doAction()
chigichan24.icon 記法がすごいしっくりこない
Go.iconアノテーションとかじゃないんだー
Go.iconAndroid開発やっていると、Contextというのがいっぱいでつらい
Entity.doAction() 内では Entity と Scope の両方のメソッド、プロパティにアクセスできそう
複数の Receiver でメソッド名などが被ったときはthis@ で Receiver を明示する
code:kotlin
context(Logger, Storage<User>)
fun userInfo(name: String): Storage<User>.Info {
this@Logger.info("Retrieving info about $name")
return this@Storage.info(name)
}
https://github.com/Kotlin/KEEP/blob/a909d00ca114d2e418d3cf9c4513a534f8ad1751/proposals/context-receivers.md#referencing-specific-receiver
使いみち
https://github.com/Kotlin/KEEP/blob/a909d00ca114d2e418d3cf9c4513a534f8ad1751/proposals/context-receivers.md#use-cases
Mori Atsushi.icon こういうのは確かに便利そう
code:kotlin
context(View)
val Float.dp get() = this * resources.displayMetrics.density
context(View)
val Int.dp get() = this.toFloat().dp
気になるポイント
コメント
いまいちモチベーションがわからん
Introduction にメンバー拡張の不便さについて書かれているのがヒントかも
https://github.com/Kotlin/KEEP/blob/a909d00ca114d2e418d3cf9c4513a534f8ad1751/proposals/context-receivers.md#introduction
(Hoge, Fuga).method のほうがわかりやすいのでは?
Compose の Modifier は特定の Composable で拡張を生やしたりしている
Mori Atsushi.icon extension乱用されがち
[chigichan24.icon] <- 記法